import 'package:flutter_module/common/widgets/title_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import '../../res/colors.dart';

class YouthToolBar extends StatefulWidget {
  const YouthToolBar({
    super.key,
    required this.body,
    this.ifNeedScaffold = true,
    this.needAppBar = true,
    this.myAppBar,
    this.titleBar,
    this.title = "",
    this.actions,
    this.resizeToAvoidBottomInset = true,
    this.extendBodyBehindAppBar = false,
    this.centerTitle = true,
    this.bodyColor = YouthColors.color_F8F7F7,
    this.needDivider = true,
    this.needBack = true,
    this.onPressed,
  });

  final Widget body;

  ///可以传自定义AppBar null则用自带的appbar
  final bool needAppBar;

  final PreferredSizeWidget? myAppBar;

  final Widget? titleBar;

  ///标题text的文本
  final String? title;

  ///标题widget
  final List<Widget>? actions;

  final bool resizeToAvoidBottomInset;

  ///返回键回调
  final VoidCallback? onPressed;

  ///默认 false，为 true 时，body 会置顶到 appbar 后，如appbar 为半透明色，可以有毛玻璃效果
  final bool extendBodyBehindAppBar;

  ///默认body背景色
  final Color bodyColor;

  /// 是否需要分割线
  final bool needDivider;

  /// 标题是否居中
  final bool centerTitle;

  /// 是否需要返回键
  final bool needBack;

  ///是否需要全屏（如果不全屏的时候，隐藏导航栏会遮挡该区域的手势，需要设置该属性为false去掉Scaffold）
  final bool ifNeedScaffold;

  @override
  State<YouthToolBar> createState() => _YouthToolBarState();
}

class _YouthToolBarState extends State<YouthToolBar> {
  @override
  Widget build(BuildContext context) {
    return widget.ifNeedScaffold
        ? Scaffold(
            extendBodyBehindAppBar: widget.extendBodyBehindAppBar,
            appBar: widget.needAppBar
                ? widget.myAppBar ??
                    TitleBar(
                      title: widget.title ?? '',
                      actions: widget.actions,
                      onBackPressed: widget.onPressed,
                      needDivider: widget.needDivider,
                      titleWidget: widget.titleBar,
                      centerTitle:
                          widget.titleBar != null ? false : widget.centerTitle,
                      needBack:
                          widget.titleBar != null ? false : widget.needBack,
                    )
                : null,
            resizeToAvoidBottomInset: widget.resizeToAvoidBottomInset,
            body: Container(
              color: YouthColors.color_F8F7F7,
              child: widget.body,
            ),
          )
        : Container(
            color: YouthColors.color_F8F7F7,
            child: widget.body,
          );
  }
}

/// 刷新组件
/// 注意1：SmartRefresher的child一定是ListView
/// onRefresh开启下拉刷新 onLoading开启下拉加载
Widget youthRefresher(
    {required RefreshController refreshController,
    required Widget? child,
    VoidCallback? onRefresh,
    VoidCallback? onLoading,
    VoidCallback? onRetry}) {
  final bool enablePullDown = onRefresh != null;
  final bool enablePullUp = onLoading != null;
  return SmartRefresher(
    enablePullDown: enablePullDown,
    enablePullUp: enablePullUp,
    controller: refreshController,
    onRefresh: onRefresh,
    onLoading: onLoading,
    header: const ClassicHeader(
      releaseIcon: Icon(Icons.arrow_upward_sharp, color: Colors.grey),
      refreshingIcon: CupertinoActivityIndicator(),
      completeDuration: Duration(milliseconds: 0),
      releaseText: "松开立即刷新",
      completeText: "刷新完成",
      idleText: "下拉可以刷新",
      failedText: "刷新失败，请重试",
      refreshingText: "正在刷新数据中...",
    ),
    footer: CustomFooter(
      builder: (BuildContext context, LoadStatus? mode) {
        Widget body;
        if (mode == LoadStatus.idle) {
          body = const Text("上拉加载");
        } else if (mode == LoadStatus.loading) {
          body = const CupertinoActivityIndicator();
        } else if (mode == LoadStatus.failed) {
          body =
              GestureDetector(onTap: onRetry, child: const Text("加载失败！点击重试！"));
        } else if (mode == LoadStatus.canLoading) {
          body = const Text("松手,加载更多!");
        } else {
          body = const Text("没有更多数据了!");
        }
        return SizedBox(
          height: 55,
          child: Center(child: body),
        );
      },
    ),
    physics: const BouncingScrollPhysics(),
    child: child,
  );
}

///自定义toast
class CustomToast extends StatelessWidget {
  const CustomToast(this.msg, {super.key});
  final String msg;

  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.center,
      child: Container(
        margin: EdgeInsets.only(bottom: 30),
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        decoration: BoxDecoration(
          color: Color(0xB3000000),
          borderRadius: BorderRadius.circular(5),
        ),
        child: //msg
            Text(msg, style: TextStyle(color: Colors.white)),
      ),
    );
  }
}
